#include<limits>
class Solution {
public:
    int min = std::numeric_limits<int>::min();

    int divide(int dividend, int divisor) {
        if (dividend == min && divisor == min) {
            return 1;
        }

        if (dividend != min && divisor != min) {
            return div(dividend, divisor);
        }
        if (divisor == min) {
            return 0;
        }
        if (divisor == neg(1)) {
            return std::numeric_limits<int>::max();
        }
        dividend = add(dividend, divisor > 0 ? divisor : neg(divisor));
        int ans = div(dividend, divisor);
        int offset = divisor > 0 ? neg(1) : 1;
        return add(ans, offset);
    }

    int div(int a, int b) {
        int x = a < 0 ? neg(a) : a;
        int y = b < 0 ? neg(b): b;

        int ans = 0;
        for (int i = 30; i >= 0; i = minus(i, 1)) {
            if ((x >> i) >= y) {
                ans |= (1 << i);
                x = minus(x, y << i);
            }
        }
        return (a < 0) ^ (b < 0) ? neg(ans) : ans;
    }
    int neg(int n) {
        return add(~n, 1);
    }
    int minus(int a, int b) {
        return add(a, neg((b)));
    }
    int add(int a, int b) {
        int ans = a;
        while (b != 0) {
            ans = a ^ b;
            b = (a & b) << 1;
            a = ans;
        }
        return ans;
    }
};